package com.ebelter.seneo.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.seneo.R;
import com.ebelter.seneo.models.SeneoDbUtils;

import java.util.List;

import static com.ebelter.seneo.models.SeneoDbUtils.getAllUserinfos;


public class MembersAdapter2 extends BaseAdapter {


    List<SeneoUserinfos> users;
    private Context context;
    public MembersAdapter2(Context context) {
        this.context=context;
        getAllUser();
    }

    private void getAllUser(){
        users = getAllUserinfos();
        if (users!=null){
            for (int i=0;i<users.size();i++){
                SeneoUserinfos userinfos = users.get(i);
                if (userinfos.host_id==userinfos.user_id){
                    users.remove(i);//把主用户添加到第一栏的意思
                    users.add(0,userinfos);
                }
            }
        }
    }

    public void removeUser(long userid){
        SeneoUserinfos userinfos =  SeneoDbUtils.getUserinfos(userid);
        if (userinfos!=null){
            userinfos.delete();
        }
        if (users!=null){
            for (int i=0;i<users.size();i++){
                SeneoUserinfos user = users.get(i);
                if (userid==user.user_id){
                    user.delete();
                    break;
                }
            }
        }

        notifyDataSetChanged();

    }


    public boolean isContain(String name){
        if (users!=null){
            if (users.size()>0){
                for (int i=0;i<users.size();i++){
                    if (TextUtils.equals(users.get(i).nickName,name)){
                        return true;
                    }
                }
            }
        }
        return false;
    }


    @Override
    public int getCount() {
        return users==null?0:users.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return users.get(position).user_id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        VH vh;
        if (convertView==null){
            vh=new VH();
            convertView = View.inflate(context, R.layout.item_members_ly,null);
            vh.iv1 = convertView.findViewById(R.id.item_mem_iv1);
            vh.tv1 = convertView.findViewById(R.id.item_mem_tv1);
            convertView.setTag(vh);
        }else {
            vh= (VH) convertView.getTag();
        }
        SeneoUserinfos userinfos=users.get(position);
        vh.iv1.setImageResource(userinfos.sex==0?R.drawable.icon_male_set:R.drawable.icon_female_set);
        vh.tv1.setText(userinfos.nickName);
        return convertView;
    }

    public void notifyDataSetChanged2() {
        getAllUser();
        notifyDataSetChanged();
    }

    class VH{
        ImageView iv1;
        TextView tv1;
    }

    public List<SeneoUserinfos> getUsers() {
        return users;
    }
}
